<?php
/**
 * RedCross Application
 *
 * @copyright TODO: Add copyright notice
 */

/**
 * Controller for the shift exchanges
 *
 * @Category RedCross
 * @package Admin_Controller
 */
class Admin_ShiftExchangeController extends Zend_Controller_Action
{
    private $_roleService;
    private $_shiftService;
    private $_shiftRoleService;
    private $_shiftExchangeService;

    public function init()
    {
        $this->_roleService = RedCross_Factory::getService('Core_Service_Role');
        $this->_shiftService = RedCross_Factory::getService('Core_Service_Shift');
        $this->_shiftRoleService = RedCross_Factory::getService('Core_Service_ShiftRole');
        $this->_shiftExchangeService = RedCross_Factory::getService('Core_Service_ShiftExchange');
    }

    public function acceptAction()
    {
        $shiftExchangeId = $this->_getParam('id');
        $validator = new Zend_Validate_Int();
        if (!$validator->isValid($shiftExchangeId)) {
            throw new RedCross_Exception_ValidationFailed();
        }
        $shiftExchangeId = (int)$shiftExchangeId;

        $shiftExchange = $this->_shiftExchangeService->fetchById($shiftExchangeId);

        if (is_null($shiftExchange)) {
            throw new RedCross_Exception_ValidationFailed('Ugyldig vaktbytteforespørsel');
        }

        $shift = $shiftExchange->ShiftRole->Shift;
        $role = $this->_shiftRoleService->fetchById($shiftExchange->ShiftRole->id);
        
        // TODO: Accept if user is a shift leader as well 
        $this->_helper->acl()->isAllowed('service:shiftRole', 'replaceRole');
        
        // do the shift exchange
        $shiftExchange->accept();
        $this->_shiftRoleService->replaceRole($role, $shiftExchange->TargetMember->id);
        // TODO: Validate that replaceRole() did its work?
        $shiftExchange->ShiftRole->notification_sent_time = null;
        $shiftExchange->ShiftRole->notification_confirmed_time = null;
        $shiftExchange->save();

        $this->sendAcceptedMail($shiftExchange);
        $this->view->flashMessage()->addMessage('Vaktbytte utført.', RedCross_View_Helper_FlashMessage::SUCCESS);

        $this->_helper->redirector('notify', 'shift', 'admin', array('id' => $shiftExchange->ShiftRole->id));
    }

    public function rejectAction()
    {
        // TODO: Accept if user is a shift leader as well 
        $this->_helper->acl()->isAllowed('service:shiftRole', 'replaceRole');
        
        // TODO: Validate shiftExchangeId
        $shiftExchangeId = $this->_getParam('id');
        $validator = new Zend_Validate_Int();
        if (!$validator->isValid($shiftExchangeId)) {
            throw new RedCross_Exception_ValidationFailed();
        }
        $shiftExchangeId = (int)$shiftExchangeId;

        $shiftExchange = $this->_shiftExchangeService->fetchById($shiftExchangeId);

        if (is_null($shiftExchange)) {
            throw new RedCross_Exception_ValidationFailed('Ugyldig vaktbytteforespørsel');
        }

        $shiftId = $shiftExchange->ShiftRole->Shift->id;

        $role = $this->_shiftRoleService->fetchById($shiftExchange->ShiftRole->id);
        
        // mark the shift exchange as rejected
        $shiftExchange->reject();

        // send mail to affected members
        $this->sendRejectionMail($shiftExchange);
        
        $this->view->flashMessage()->addMessage('Vaktbytte avslått', RedCross_View_Helper_FlashMessage::SUCCESS);
        $this->_helper->redirector('edit', 'shift', 'admin', array('id' => $shiftId));
    }


    public function sendAcceptedMail($shiftExchange) {
        $mail = new RedCross_Mail_Base();
        $mail->setSubject('Forespørsel om vaktbytte godkjent');
        $content = new Zend_View();
        $content->setScriptPath(APPLICATION_PATH . '/layouts/main/emails/');

        $shift = $shiftExchange->ShiftRole->Shift;

        $source = $shiftExchange->SourceMember;
        $target = $shiftExchange->TargetMember;

        $mail->addTo($source->getMainEmail()->address, $source->getFullName());
        $mail->addTo($target->getMainEmail()->address, $target->getFullName());

        $content->assign('sourceMember', $source);
        $content->assign('targetMember', $target);
        $content->assign('shift', $shift);
        $content->assign('event', $shift->Event);

        $body = $content->render('shift-exchange-accepted.phtml');
        try {
            $mail->setBodyText($body);
            $mail->send();
        } catch (Exception $e) {
            $this->view->flashMessage()->addMessage('Feilet å sende mail (' . $e->getMessage() . 
                ')', RedCross_View_Helper_FlashMessage::ERROR);
        }
    }

    public function sendRejectionMail($shiftExchange) {
        $mail = new RedCross_Mail_Base();
        $mail->setSubject('Forespørsel om vaktbytte avslått');
        $content = new Zend_View();
        $content->setScriptPath(APPLICATION_PATH . '/layouts/main/emails/');

        $shift = $shiftExchange->ShiftRole->Shift;

        $source = $shiftExchange->SourceMember;
        $target = $shiftExchange->TargetMember;

        $mail->addTo($source->getMainEmail()->address, $source->getFullName());
        $mail->addTo($target->getMainEmail()->address, $target->getFullName());

        $content->assign('sourceMember', $source);
        $content->assign('targetMember', $target);
        $content->assign('shift', $shift);
        $content->assign('event', $shift->Event);

        $body = $content->render('shift-exchange-rejection.phtml');
        try {
            $mail->setBodyText($body);
            $mail->send();
        } catch (Exception $e) {
            $this->view->flashMessage()->addMessage('Feilet å sende mail (' . $e->getMessage() . 
                ')', RedCross_View_Helper_FlashMessage::ERROR);
        }
    }
}

?>
